package com.zhdl.hardware_modules.web.controller;

import com.project.common.util.DelayUtil;
import com.zhdl.common.util.WebResult;
import com.zhdl.hardware.io.smartLink.service.IOCmdSendService;
import com.zhdl.hardware.motor.buke.repository.impl.BukePortCmdHandle;
import com.zhdl.hardware.motor.huichuan.service.HCCmdSendService;
import com.zhdl.hardware.motor.leisai.service.LSCmdSendService;
import com.zhdl.hardware.plc.siemens.repository.SiemensCmdHandleDao;
import com.zhdl.hardware.plc.siemens.service.SiemensCmdSendService;
import com.zhdl.hardware.robot.borunte.repository.impl.BorunteCmdHandleDaoImpl;
import com.zhdl.hardware.robot.borunte.service.BorunteCmdSendService;
import com.zhdl.hardware_modules.service.DeviceService;
import com.zhdl.hardware_modules.web.response.DeviceInfoResp;
import com.zhdl.hardware_modules.web.response.ProcessSystemResp;
import com.zhdl.modules.wallpulley.web.WallPulleyProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hardwareDebug")
public class HardwareDebugController {

    @Resource
    DeviceService deviceService;

    @Resource
    private HCCmdSendService hcCmdSendService;

    @Resource
    private BukePortCmdHandle bukePortCmdHandle;

    @Resource
    private IOCmdSendService ioCmdSendService;

    @Resource
    private BorunteCmdSendService borunteCmdSendService;

    @Resource
    private LSCmdSendService lsCmdSendService;

    /**
     * 硬件调试-展示所有设备信息
     *
     * @return 设备详情信息
     */
    @GetMapping("/deviceList")
    public WebResult<List<DeviceInfoResp>> findAllDeviceInfo() {
        List<DeviceInfoResp> allDevice = deviceService.findAllDevice();
        return WebResult.okResult(allDevice);
    }

    /**
     * 硬件调试-查询所有流程相关步数
     *
     * @param id 当前系统Id
     * @return 当前系统所有流程步数
     */
    @GetMapping("/deviceStepList")
    public WebResult<ProcessSystemResp> findAllSystemStep(int id) {
        ProcessSystemResp allSystemStep = deviceService.findAllSystemStep(id);
        return WebResult.okResult(allSystemStep);
    }

    @GetMapping("/deviceEditStepParam")
    public WebResult<String> deviceEditStepParam(int id, String param) {
        deviceService.setStepParam(id, param);
        return WebResult.okResult("set ok!!!");
    }

    /*
        ==========================通用设备-调试相关==========================
     */

    /**
     * 流程开启
     */
    @GetMapping("/processStart")
    public WebResult<String> processStart(int id) {
//        if (id == 1){
//            filterProcess.init();
//        } else if (id == 2){
//            eastDosingProcess.eastDosingProcess(id);
//        } else if (id == 3){
//            processControl.processEntryByCassetteId(id);
//        } else if (id == 4){
//            slagRemovalProcess.processAutomaticMode();
//        }
        return WebResult.okResult("process-" + id + ",already start!");
    }

    /*
        ==========================汇川电机-调试相关==========================
     */

    /**
     * 汇川电机-电机停止
     *
     * @param port    串口号
     * @param slaveId 从站Id
     * @return 结果
     */
    @GetMapping("/hcMotorStop")
    public WebResult<String> hcMotorStop(String port, String slaveId) {
        hcCmdSendService.motorStatusStop(port, slaveId);
        return WebResult.okResult("Motor port is:" + port + ",slaveId is " + slaveId + ", already stop success!");
    }

    /**
     * 汇川电机-电机使能
     *
     * @param port    串口号
     * @param slaveId 从站Id
     * @return 结果
     */
    @GetMapping("/hcMotorReady")
    public WebResult<String> hcMotorReady(String port, String slaveId) {
        hcCmdSendService.motorStatusReady(port, slaveId);
        return WebResult.okResult("Motor port is:" + port + ",slaveId is " + slaveId + ", already ready success!");
    }

    /**
     * 汇川电机-电机回原点
     *
     * @param port    串口号
     * @param slaveId 从站Id
     * @param speed   速度
     * @return 结果
     */
    @GetMapping("/hcMotorHoming")
    public WebResult<String> hcMotorHoming(String port, String slaveId, int speed) {
        hcCmdSendService.motorMoveHoming(port, slaveId, speed);
        return WebResult.okResult("Motor port is:" + port + ",slaveId is " + slaveId + ", already homing success!");
    }

    /**
     * 汇川电机-电机运动
     *
     * @param port    串口号
     * @param slaveId 从站Id
     * @return 结果
     */
    @GetMapping("/hcMotorRun")
    public WebResult<String> hcMotorRun(String port, String slaveId, int distance, int speed) {
        hcCmdSendService.motorMoveDistance(port, slaveId, distance, speed, s -> {
            log.info("run ok");
        });
        return WebResult.okResult("Motor port is:" + port + ",slaveId is " + slaveId + ", set distance and speed is " + distance + "," + speed + ", run success!");
    }

    /*
        ==========================步科电机-调试相关==========================
     */

    /**
     * 步科电机-电机停止
     *
     * @param port
     * @param slaveId
     * @return
     */
    @GetMapping("/bkMotorStop")
    public WebResult<String> bkMotorStop(String port, String slaveId) {
        bukePortCmdHandle.bukeMotorStop(port, slaveId);
        return WebResult.okResult("Motor port is:" + port + ",slaveId is " + slaveId + ", already stop success!");
    }

    /**
     * 步科电机-电机停止
     *
     * @param port
     * @param slaveId
     * @param distance
     * @param speed
     * @return
     */
    @GetMapping("/bkMotorRun")
    public WebResult<String> bkMotorRun(String port, String slaveId, int distance, int speed) {
        bukePortCmdHandle.bukeMotorRun(port, slaveId, distance, speed);
        return WebResult.okResult("Motor port is:" + port + ",slaveId is " + slaveId + ", already stop success!");
    }

    /*
        ========================== 电机-调试相关==========================
     */

    @Resource
    private WallPulleyProcess wallPulleyProcess;

    /**
     * 雷赛电机 回零
     */
    @GetMapping("/lsMotorBack")
    public WebResult<String> lsMotorBack() {
        lsCmdSendService.leisaiSetMoveDist("2", "01", 0);
        DelayUtil.delayedExecution(1000, () -> wallPulleyProcess.releaseBackOrigin());
        return WebResult.okResult("Motor port is:" + "2" + ",slaveId is " + "01" + ", already back origin success!");
    }
    /**
     * 雷赛电机 急停
     */
    @GetMapping("/lsMotorEmergencyStop")
    public WebResult<String> lsMotorEmergencyStop() {
        lsCmdSendService.leisaiEmergencyStop("2", "02");
        return WebResult.okResult("Motor port is:" + "2" + ",slaveId is " + "01" + ", already Emergency Stop success!");
    }

    /*
        ==========================SmartLink IO-调试相关==========================
     */
    @GetMapping("/smartWriteIO")
    public WebResult<String> smartWriteIO(int address, boolean open) {
        ioCmdSendService.ioWriteCoil05(address, open, s -> {
            log.info("write ok");
        });
        return WebResult.okResult("IO write address-" + address + ", open is " + open + "success!");
    }

    /*
        ==========================Borunte Robot-调试相关==========================
     */

    /**
     * 机器人流程 - 设置Z轴高度
     */
    @GetMapping("/borunteRobotSetAxisZ")
    public WebResult<String> robotSetAxisZ(int locationZ) {
        BorunteCmdHandleDaoImpl.originZ = locationZ;
        return WebResult.okResult("Robot Z-axis set up: " + locationZ + "success!");
    }

    /**
     * 机器人流程 - 设置UVW轴高度
     */
    @GetMapping("/borunteRobotSetAxisUVW")
    public WebResult<String> robotSetAxisZ(int locationU, int locationV, int locationW) {
        int[] locationUVW = {locationU, locationV, locationW};
        BorunteCmdHandleDaoImpl.UVW0 = locationUVW;
        return WebResult.okResult("Robot Z-axis set up: " + Arrays.toString(locationUVW) + "success!");
    }

    /**
     * 机器人流程 - 初始化位置
     */
    @GetMapping("/borunteRobotInitLocation")
    public WebResult<String> robotInitLocation(int locationX, int locationY, int locationZ) {
        //获取机器人需要走动的坐标
        int[] location = {locationX, locationY, locationZ};
        borunteCmdSendService.borunteSetOriginZ(s -> {
            log.info("设置机器人Z轴坐标高度完成");
        });
        return WebResult.okResult("Robot init location set up: " + Arrays.toString(location) + "success!");
    }

    /**
     * 机器人流程 - 抓料点位置
     */
    @GetMapping("/borunteRobotPickLocation")
    public WebResult<String> robotPickLocation(int locationX, int locationY, int locationZ) {
        //获取机器人需要走动的坐标
        int[] location = {locationX, locationY, locationZ};
        borunteCmdSendService.borunteSetPickLocation(location, s -> {
            log.info("设置机器人Z轴坐标高度完成");
        });
        return WebResult.okResult("Robot pick location set up: " + Arrays.toString(location) + "success!");
    }

    /**
     * 机器人流程 - 信号交互
     */
    @GetMapping("/borunteRobotSendM00")
    public WebResult<String> robotSendM00(int robotStatusM) {
        //获取地址，直接发送指令
        borunteCmdSendService.borunteWriteStatusM(robotStatusM, true);
        return WebResult.okResult("Send robot signal : " + robotStatusM + "success!");
    }

    /**
     * 机器人流程 - 暂停与启动
     */
    @GetMapping("/borunteRobotPause")
    public WebResult<String> robotSetPause(boolean pause) {
        //设置机器人暂停运动
//        cmdHandleService.robotSetPause(pause);
        return WebResult.okResult("robot already pause status is " + pause);
    }

}
